import { _decorator, Color, Component, director, Label, Node, Sprite, Vec2, Vec3 } from 'cc';
import { aa } from '../frame/FrameCore';
import { RoomData } from '../deco/RoomBuilder';
const { ccclass, property } = _decorator;
const temp_v2 = new Vec2();
const normal = Color.WHITE;
const pressed = new Color(120, 160, 240, 255);

@ccclass('RoomViewCtrl')
export class RoomViewCtrl extends Component {
    @property(Label)
    roomName: Label = null;
    @property(Node)
    bgNode: Node = null;
    @property(Sprite)
    bgDeco: Sprite = null;
    private _pos: Vec3 = new Vec3();
    private _scale = 1;
    private _index: number = 0;
    private _pressed = false

    set press(v) {
        if (v == this._pressed) return;
        this._pressed = v;
        this.roomName.color = this.bgDeco.color = v ? pressed : normal;
        this.bgNode.setPosition(v ? 0 : -26, 0)
    }
    get press() {
        return this._pressed;
    }

    onEnable() {
        // this.press = false;

        this.node.on(Node.EventType.TOUCH_END, this.onClick, this)
    }
    protected onDisable(): void {
        this.press = false;
        this.node.off(Node.EventType.TOUCH_END, this.onClick, this)
    }


    init(name: string, width: number, position: { x: number, y: number }, index: number) {
        this._index = index
        this.roomName.string = name;
        this._scale = width;
        this._pos.set(position.x, 0.35, position.y)
    }

    onClick() {
        this.press = true;
        const cam = aa.global.orbitCam;
        director.emit("RoomSelect", this._index)
        if (aa.global.isGame) {
            aa.global.playerNode.setPosition(this._pos.x,0,this._pos.z)
        } else {
            cam.orbitTarget.node.setPosition(this._pos);
            const bestRadius = this._scale;
            cam.radius = bestRadius;
            cam.radiusRange = temp_v2.set(Math.max(bestRadius - 6, 0.5), bestRadius + 3)
            cam.resetTargetRotation();

        }

    }

}

